// Fill out your copyright notice in the Description page of Project Settings.


#include "Test/PopupBox/Toast/SToastSlate.h"
#include "SlateOptMacros.h"
#include "Widgets/Layout/SConstraintCanvas.h"
#include "Widgets/Images/SImage.h"
#include "Widgets/Input/SMultiLineEditableTextBox.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SToastSlate::Construct(const FArguments& InArgs)
{
	TextBoxStyle = new FEditableTextBoxStyle();
	TextBoxStyle->SetScrollBarStyle(*InArgs._ScrollBrush);
	TextBoxStyle->SetBackgroundImageReadOnly(*InArgs._TextBackground);
	TextBoxStyle->SetPadding(FMargin(0.f,20.f));
	
	ChildSlot
	[
		SNew(SConstraintCanvas)
		
		+ SConstraintCanvas::Slot()
		.Anchors(FAnchors( 0.f, 0.f, 1.f, 1.f ))
		.AutoSize(true)
		[
			SNew(SBox)
			.HAlign(EHorizontalAlignment::HAlign_Fill)
			.VAlign(EVerticalAlignment::VAlign_Fill)
			[
				SNew(SOverlay)

				+SOverlay::Slot()
				.HAlign(EHorizontalAlignment::HAlign_Fill)
				.VAlign(EVerticalAlignment::VAlign_Fill)
				[
					SAssignNew(ToastMask, SImage)
					.Visibility(InArgs._ShowMask ? EVisibility::Visible : EVisibility::Collapsed)
					.Image(InArgs._MaskBrush)
				]

				+SOverlay::Slot()
				.HAlign(EHorizontalAlignment::HAlign_Center)
				.VAlign(EVerticalAlignment::VAlign_Center)
				[
					SAssignNew(OutToastBox,SBox)
					.HAlign(EHorizontalAlignment::HAlign_Fill)
					.VAlign(EVerticalAlignment::VAlign_Fill)
					.MaxDesiredWidth(InArgs._ToastSize.X)
					.MaxDesiredHeight(InArgs._ToastSize.Y)
					[
						SAssignNew(ToastBox, SBorder)
						.HAlign(EHorizontalAlignment::HAlign_Fill)
						.VAlign(EVerticalAlignment::VAlign_Fill)
						.BorderImage(InArgs._ToastBrush)
						.Padding(InArgs._ToastPadding)
						[
							SNew(SVerticalBox)

							+SVerticalBox::Slot()
							.Padding(FMargin(0.f))
							.AutoHeight()
							.HAlign(EHorizontalAlignment::HAlign_Center)
							.VAlign(EVerticalAlignment::VAlign_Center)
							[
								SAssignNew(ToastIconBox,SBox)
								.MaxDesiredWidth(InArgs._IconSize.X)
								.MaxDesiredHeight(InArgs._IconSize.Y)
								.HAlign(EHorizontalAlignment::HAlign_Fill)
								.VAlign(EVerticalAlignment::VAlign_Fill)
								[
									SAssignNew(ToastIcon,SImage)
									.Visibility(InArgs._ShowIcon ? EVisibility::Visible : EVisibility::Collapsed)
									.Image(InArgs._IconBrush)
								]
							]

							+SVerticalBox::Slot()
							.Padding(FMargin(0.f))
							.FillHeight(1.f)
							.HAlign(EHorizontalAlignment::HAlign_Fill)
							.VAlign(EVerticalAlignment::VAlign_Fill)
							[
								SAssignNew(ToastTextBox, SMultiLineEditableTextBox)
								.Text(InArgs._ToastText)
								.IsReadOnly(true)
								.Style(TextBoxStyle)
								// .TextStyle(InArgs._TextFont)
								.Justification(ETextJustify::Center)
								.WrapTextAt(InArgs._ToastSize.X)
								.WrappingPolicy(ETextWrappingPolicy::AllowPerCharacterWrapping)
							]
						]
					]
				]
			]
		]
	];
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SToastSlate::SetShowMask(bool InShowMask)
{
	ToastMask->SetVisibility(InShowMask ? EVisibility::Visible : EVisibility::Collapsed);
}

void SToastSlate::SetMaskBrush(const FSlateBrush* InMaskBrush)
{
	ToastMask->SetImage(InMaskBrush);
}

void SToastSlate::SetShowIcon(bool InShowIcon)
{
	ToastIcon->SetVisibility(InShowIcon ? EVisibility::Visible : EVisibility::Collapsed);
}

void SToastSlate::SetIconBrush(const FSlateBrush* InIconBrush)
{
	ToastIcon->SetImage(InIconBrush);
}

void SToastSlate::SetIconSize(const FVector2D& InIconSize)
{
	ToastIconBox->SetMaxDesiredWidth(InIconSize.X);
	ToastIconBox->SetMaxDesiredHeight(InIconSize.Y);
}

void SToastSlate::SetToastText(const TAttribute<FText>& InToastText)
{
	ToastTextBox->SetText(InToastText);
}

void SToastSlate::SetTextFont(const FTextBlockStyle* InTextFont)
{
	ToastTextBox->SetTextStyle(InTextFont);
}

void SToastSlate::SetTextBackground(const FSlateBrush* InTextBackground)
{
	TextBoxStyle->SetBackgroundImageReadOnly(*InTextBackground);
}

void SToastSlate::SetScrollBrush(const FScrollBarStyle* InScrollBrush)
{
	TextBoxStyle->SetScrollBarStyle(*InScrollBrush);
}

void SToastSlate::SetToastSize(const FVector2D& InToastSize)
{
	OutToastBox->SetMaxDesiredWidth(InToastSize.X);
	OutToastBox->SetMaxDesiredHeight(InToastSize.Y);
	ToastTextBox->SetWrapTextAt(InToastSize.X);
}

void SToastSlate::SetToastPadding(const FMargin& InToastPadding)
{
	ToastBox->SetPadding(InToastPadding);
}

void SToastSlate::SetToastBrush(const FSlateBrush* InToastBrush)
{
	ToastBox->SetBorderImage(InToastBrush);
}
